import { defineStore } from "pinia";
import { ref } from "vue";
import { getUserDetails } from "@/api/user";

// 用户模块
export const useUserStore = defineStore(
  "big-user",
  () => {
    const token = ref(""); // 定义 token
    const setToken = (t) => (token.value = t); // 设置 token
    // 清空
    const clear = () => {
      token.value = "";
    };

    //定义用户信息
    const userInfo = ref({});
    //获取用户信息
    const getUserInfo = async () => {
      const res = await getUserDetails();
      userInfo.value = res.data.data;
    };
    // 清空用户数据
    const clearUserInfo = () => {
      userInfo.value = {};
    };

    return { token, setToken, clear, userInfo, getUserInfo, clearUserInfo };
  },
  {
    persist: true, // 持久化
  }
);
